package cn.itsource.system.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.system.domain.Menu;
import cn.itsource.system.query.MenuQuery;
import cn.itsource.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    /**
     * 接口：添加或修改
     *
     * @param menu 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Menu menu) {
        if (menu.getId() != null) {
            menuService.update(menu);
        } else {
            menuService.save(menu);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        menuService.deleteById(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        menuService.batchDeleteByIds(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Menu findOne(@PathVariable("id") Long id) {
        return menuService.selectById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping("/all")
    public List<Menu> findAll() {
        return menuService.selectAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/pageList")
    public AjaxResult queryPage(@RequestBody MenuQuery query) {
        return AjaxResult.me().setResultObj(menuService.queryPage(query));
    }

    @GetMapping("/getMenuTree")
    public AjaxResult getMenuTree() {
        return menuService.getMenuTree();
    }
}
